package nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author RJA
 * @version V1.0
 * @Title: nio.demo_1
 * @Description:
 * @date 2019/9/5 19:05
 */
public class NioTest08 {
    public static void main(String[] args) {
        try(FileInputStream inputStream = new FileInputStream("input2.txt");
            FileOutputStream outputStream = new FileOutputStream("output2.txt")){

            FileChannel inputChannel = inputStream.getChannel();
            FileChannel outputChannel = outputStream.getChannel();

            ByteBuffer buffer = ByteBuffer.allocateDirect(512);

            while (true){
                buffer.clear();

                int read = inputChannel.read(buffer);

                if(read == -1){
                    break;
                }

                buffer.flip();
                outputChannel.write(buffer);
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
